package com.xiaoyy.core.common.utils;

import com.xiaoyy.core.common.exception.AppException;
import com.xiaoyy.core.common.vo.AntTreeVO;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* ant vue树工具类
 * @author chenzh
 * @date 2018-08-14
* **/
public class AntTreeUtil {
    public static List<AntTreeVO> constructTaskDTOToTree(List<AntTreeVO> taskDTOList) {
        return constructTaskDTOToTree(taskDTOList,null);
    }
        /**
         * 将List重组为数
         * @param taskDTOList DTO集合
         * @return List<TreeEntity>
         */
    public static List<AntTreeVO> constructTaskDTOToTree(List<AntTreeVO> taskDTOList,String resultTask){
        if (CollectionUtils.isEmpty(taskDTOList)){
            return new ArrayList<>();
        }
        //key:父节点ID value:子节点集合
        Map<String,List<AntTreeVO>> taskDTOMap = new HashMap<>();

        //将List重组到Map中
        taskDTOList.forEach(dto -> {
            List<AntTreeVO> tempTaskDTOList = taskDTOMap.get(dto.getParentKey());
            if (tempTaskDTOList == null){
                tempTaskDTOList = new ArrayList<AntTreeVO>();
                taskDTOMap.put(dto.getParentKey(),tempTaskDTOList);
            }
            tempTaskDTOList.add(dto);
        });

        //顶级节点集合

        List<AntTreeVO> resultTaskDTOList = taskDTOMap.get(resultTask);
        if (CollectionUtils.isEmpty(resultTaskDTOList)){
            resultTaskDTOList = taskDTOMap.get("#");
        }

        recurTaskDTOList(resultTaskDTOList,taskDTOMap);

        return resultTaskDTOList;
    }

    /**
     * 将重组好的Map进行树形结构处理
     * @param taskDTOList 父节点集合(不一定是顶级节点 因为会递归调用)
     * @param sourceMap 组装好的Map集合
     */
    public static void recurTaskDTOList(List<AntTreeVO> taskDTOList,Map<String,List<AntTreeVO>> sourceMap){
        if(CollectionUtils.isEmpty(taskDTOList)) {
            return;
        }
        taskDTOList.forEach(dto -> {
            dto.setChildren(sourceMap.get(dto.getKey()));
            recurTaskDTOList(dto.getChildren(),sourceMap);
        });
    }
    public static List<String> getChildKeys(List<AntTreeVO> taskDTOList,String key){
        List<String> list = new ArrayList<>();
        if (CollectionUtils.isEmpty(taskDTOList)){
            throw new AppException("未传入数据");
        }
        getChildKeys(taskDTOList,key,list,false);
        return list;
    }
    public static boolean getChildKeys(List<AntTreeVO> taskDTOList,String key,List<String> list,boolean flag){
        if (flag){
            for (AntTreeVO antTreeVO:taskDTOList){
                list.add(antTreeVO.getKey());
                if (!CollectionUtils.isEmpty(antTreeVO.getChildren())){
                    getChildKeys(antTreeVO.getChildren(),key,list,flag);
                }
            }
        }else{
            for (AntTreeVO antTreeVO:taskDTOList){
                if (antTreeVO.getKey().equalsIgnoreCase(key)){
                    list.add(key);
                    flag = true;
                    if (!CollectionUtils.isEmpty(antTreeVO.getChildren())){
                        getChildKeys(antTreeVO.getChildren(),key,list,flag);
                    }
                    break;
                }
            }
            if (!flag){
                for (AntTreeVO antTreeVO:taskDTOList){
                    if (!CollectionUtils.isEmpty(antTreeVO.getChildren())){
                        if (getChildKeys(antTreeVO.getChildren(),key,list,flag)){
                            break;
                        }
                    }
                }
            }
        }
        return flag;
    }
    public static List<String> getParentKeys(List<AntTreeVO> taskDTOList,String key){
        List<String> list = new ArrayList<>();
        if (CollectionUtils.isEmpty(taskDTOList)){
            throw new AppException("未传入数据");
        }
        getParentKeys(taskDTOList,key,list);
        return list;

    }
    public static Boolean getParentKeys(List<AntTreeVO> taskDTOList,String key,List<String> list){
        if (CollectionUtils.isEmpty(taskDTOList)){
            return false;
        }
        boolean flag = false;
        for (AntTreeVO antTreeVO:taskDTOList){
            if (antTreeVO.getKey().equalsIgnoreCase(key)){
                flag = true;
                list.add(key);
                break;
            }
        }
        if (!flag){
            for (AntTreeVO antTreeVO:taskDTOList){
                if (!CollectionUtils.isEmpty(antTreeVO.getChildren())){
                    flag = getParentKeys(antTreeVO.getChildren(),key,list);
                    if (flag){
                        list.add(antTreeVO.getKey());
                        break;
                    }
                }
            }
        }
        return flag;

    }
}